using System;
using System.Collections.Generic;

namespace ICSharpCode.TextEditor.Formatting
{
    public class JsonFormatStrategy : IFormattingStrategy
    {
        private string ConvertJsonString(string strjson, Dictionary<string, string> arrepl = null)
        {
            string text = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(strjson))
                {
                    return strjson;
                }
                int num = 0;
                strjson = strjson.Replace('\r', ' ').Replace('\n', ' ');
                char[] array = strjson.ToCharArray();
                List<char> list = new List<char>();
                int num2 = 0;
                bool flag = true;
                int num3 = 0;
                bool flag2 = true;
                int i = 0;
                for (int num4 = array.Length; i < num4; i++)
                {
                    char c = array[i];
                    if (c == '"')
                    {
                        num2 = ((num2 == 0) ? 1 : 0);
                    }
                    flag = ((num2 == 0) ? true : false);
                    if (c == '\'')
                    {
                        num3 = ((num3 == 0) ? 1 : 0);
                    }
                    flag2 = ((num3 == 0) ? true : false);
                    if (c != ' ')
                    {
                        if (c != '{' && c != '}' && c != ',' && c != '[' && c != ']' && c != '*')
                        {
                            list.Add(c);
                            continue;
                        }
                        if (c == '{' || c == '[')
                        {
                            list.Add(c);
                            list.Add('\n');
                        }
                        if (c == '}' || c == ']')
                        {
                            list.Add('\n');
                            list.Add(c);
                        }
                        if (c == ',')
                        {
                            list.Add(c);
                            if (flag && flag2)
                            {
                                list.Add('\n');
                            }
                        }
                        if (c == '*')
                        {
                            bool flag3 = false;
                            if (i > 0 && array[i - 1] == '/')
                            {
                                list.Add(c);
                                list.Add('\n');
                                flag3 = true;
                            }
                            if (i < num4 - 1 && array[i + 1] == '/')
                            {
                                list.Add('\n');
                                list.Add(c);
                                flag3 = true;
                            }
                            if (!flag3)
                            {
                                list.Add(c);
                            }
                        }
                    }
                    else if (i > 0 && array[i - 1] != ' ' && i < num4 - 1 && array[i + 1] != ' ')
                    {
                        list.Add(c);
                    }
                }
                string text2 = string.Concat(list.ToArray());
                if (arrepl != null)
                {
                    foreach (string key in arrepl.Keys)
                    {
                        text2 = text2.Replace(key, arrepl[key]);
                    }
                }
                string text3 = "   ";
                string[] array2 = text2.Split('\n');
                string[] array3 = array2;
                foreach (string text4 in array3)
                {
                    string text5 = text4.ToString();
                    if (text4 == "{" || text4 == "[")
                    {
                        num++;
                        for (int k = 1; k < num; k++)
                        {
                            text5 = text3 + text5;
                        }
                    }
                    if (text4 == "}" || text4 == "]")
                    {
                        for (int l = 1; l < num; l++)
                        {
                            text5 = text3 + text5;
                        }
                        num--;
                    }
                    if (text4.Contains("},"))
                    {
                        for (int m = 1; m < num; m++)
                        {
                            text5 = text3 + text5;
                        }
                        num--;
                    }
                    if (text4.Contains("],"))
                    {
                        for (int n = 1; n < num; n++)
                        {
                            text5 = text3 + text5;
                        }
                        num--;
                    }
                    if (text4.Contains(":[") || text4.Contains(":{"))
                    {
                        for (int num5 = 0; num5 < num; num5++)
                        {
                            text5 = text3 + text5;
                        }
                        num++;
                    }
                    if (text4.ToLower().Contains("){"))
                    {
                        num++;
                    }
                    if (text4 != "[" && text4 != "{" && text4 != "}" && text4 != "]" && text4 != "}," && text4 != "]," && !text4.Contains(":[") && !text4.Contains(":{"))
                    {
                        for (int num6 = 0; num6 < num; num6++)
                        {
                            text5 = text3 + text5;
                        }
                    }
                    text = text + text5 + "\r\n";
                }
                text2 = null;
                list.Clear();
                list = null;
            }
            catch (Exception)
            {
                text = strjson;
            }
            return text;
        }

        public string StringFormat(string content)
        {
            string empty = string.Empty;
            try
            {
                return ConvertJsonString(content);
            }
            catch (Exception)
            {
                return content;
            }
        }
    }
}
